# 
#  Copyright (C) 2009 Vadim Ushakov
#  
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or (at
#  your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
#  General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

set mode_kernel 0
if { [lsearch $argv "--kernel"] != -1 } then { set mode_kernel 1 }

set a [read stdin]

set index 0
set max_table 0

foreach line [split $a "\n"] {

	if { [regexp {\A[ \t]*([0-9]+)[ \t\n]*\Z} $line f_val] != 0 } then {
		set index $f_val
		set table_[expr $index / 1000] {}
		set max_table [expr ($index / 1000) > $max_table ? $index / 1000 : $max_table]
	} elseif { [regexp {\A[ \t]*([0-9a-zA-Z_]+)[ \t\n]*\Z} $line f_name] != 0 } then {
	
		if {$mode_kernel} then {
			lappend table_[expr $index / 1000] $f_name
		} else {
			puts "#define _fnum_$f_name $index"
		}
		incr index
	}

}


set s_tables ""
set s_table_index ""


if {$mode_kernel} then {
	for {set t 0} {$t <= $max_table} {incr t} {
		if {[info exists table_$t]} then {
			set table [expr \$table_$t]
			set tabel_size [llength $table]
			set tabel_name "functionTable_$t"
			append s_tables "static kernelFunctionIndex ${tabel_name}\[\] = {\n"
			foreach f $table {
				append s_tables "	SYSCALL_FunctionIndex_$f,\n"
			}
			append s_tables "	{ 0, NULL, 0, 0, 0, 0, NULL }\n"
			append s_tables "};\n"
		} else {
			set tabel_size 0
			set tabel_name "NULL"
		}
		append s_table_index "	{$tabel_name, $tabel_size},\n"
	}

	puts $s_tables
	puts "static struct {kernelFunctionIndex* table; unsigned size;} functionTables\[\] = {"
	puts $s_table_index
	puts "	{NULL, 0}"
	puts "};"
	puts "#define MAX_FUNCTION_TABLE $max_table"
}


